// Copyright DustedPixels.com 2008. All rights reserved.

package com.dustedpixels.jasmin;

/**
 * @author micapolos@gmail.com (Michal Pociecha-Los)
 */
public class BooleanVsIntegerBenchmark {
  private static final int CYCLES = 140000000;

  public static void main(String[] args) {
    int int1 = 0;
    int int2 = 0;
    int int3 = 0;
    int int4 = 0;
    int int5 = 0;
    boolean bool1 = false;
    boolean bool2 = false;
    boolean bool3 = false;
    boolean bool4 = false;
    boolean bool5 = false;
    
    for (int i = 0; i < CYCLES; i++) {
      int1 = ~int1;
      int2 = ~int1;
      int3 = int1 & int2;
      int4 = int1 | int2;
      int5 = int1 ^ int2;
    }
    
    long intTime = System.currentTimeMillis();
    for (int i = 0; i < CYCLES; i++) {
      int1 = ~int1;
      int2 = ~int1;
      int3 = int1 & int2;
      int4 = int1 | int2;
      int5 = int1 ^ int2;
    }
    intTime = System.currentTimeMillis() - intTime;

    for (int i = 0; i < CYCLES; i++) {
      bool1 = !bool1;
      bool2 = !bool1;
      bool3 = bool1 && bool2;
      bool4 = bool1 || bool2;
      bool5 = bool1 != bool2;
    }
    
    long boolTime = System.currentTimeMillis();
    for (int i = 0; i < CYCLES; i++) {
      bool1 = !bool1;
      bool2 = !bool1;
      bool3 = bool1 && bool2;
      bool4 = bool1 || bool2;
      bool5 = bool1 != bool2;
    }
    boolTime = System.currentTimeMillis() - boolTime;
    
    System.out.println("Integer logic: " + intTime + "ms");
    System.out.println("Boolean logic: " + boolTime + "ms");
    if (intTime == boolTime) {
      System.out.println("Integer and boolean logic are equally fast");
    } else if (intTime > boolTime) {
      System.out.println("Boolean logic is faster " + ((float) intTime / boolTime) + " times");
    } else {
      System.out.println("Integer logic is faster " + ((float) boolTime / intTime) + " times");
    }
  }
}
